<?php
class UserGroupService extends Service {
    
    function __construct() {
        parent::__construct();
        $this->mDao = new UserGroupDao();
    }

    public function insertObject($obj){
        try {
            $newId = str_replace(" ", "_", strtolower($obj->getName()));
            $obj->setId($newId);
            if($this->mDao->getObject($obj->getId())!=null){
                throw new Exception("Entry with id [$newId] is Exist.");
            }
            $this->mDao->beginTrans();
            
                $Id = $this->mDao->insertObject($obj);
                if ($Id==null) return null;
                if($obj->getPrivilegeInfo()!=null){
                    $privilegeInfoDao = new PrivilegeInfoDao();
                    foreach($obj->getPrivilegeInfo() as $item){
                        $Group = new UserGroup();
                        $Group->setId($Id);
                        $Group->IsLoaded(true);
                        $item->setGroup($Group);
                        
                        if($privilegeInfoDao->insertObject($item)==null) return null;
                    }
                }
            $this->mDao->commitTrans();
            
            return $Id;
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            $this->mDao->rollback();
        }
    }

    public function updateObject($obj,$oldId){
        try {
            $this->mDao->beginTrans();
                $newId = str_replace(" ", "_", strtolower($obj->getName()));
                $obj->setId($newId);
                if($obj->getId()!=$oldId){
                    if($this->mDao->getObject($obj->getId())!=null){
                        throw new Exception("Duplicate entry for id [$newId].");
                    }
                }
                if ($this->smartUpdate($obj,$oldId)==false) return null;
                
            $this->mDao->commitTrans();

            return true;
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            $this->mDao->roolback();
            throw new Exception($exc->getMessage());
        }
    }

    public function getList() {
        try {
            return $this->mDao->getList();
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            throw new Exception($exc->getMessage());
        }
    }
    
    public function getObject($id){
        try {
            return $this->mDao->getObject($id);
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            throw new Exception($exc->getMessage());
        }
    }

    public function deleteObject($id){
        try {
            $userGroup = $this->mDao->getObject($id);
            $this->mDao->beginTrans();
                if($userGroup!=null){               
                    $privilegeInfoDao = new PrivilegeInfoDao();
                    if($userGroup->getPrivilegeInfo()!=null){
                        foreach ($userGroup->getPrivilegeInfo() as $item){
                            if($privilegeInfoDao->deleteObject($item->getId())==false) return false;
                        }
                    }
                    if($this->mDao->deleteObject($userGroup->getId())==false) return false;
                }
                
            $this->mDao->commitTrans();
            return true;
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            $this->mDao->roolback();
            throw new Exception($exc->getMessage());
        }
    }
    
    public function smartUpdate($obj,$oldId){
        if($this->mDao->updateObject($obj,$oldId)==false) return false;
        
        $userService = new UserService();
        if($userService->updateGroupId($obj->getId(), $oldId)==false) return false;
        
        $privilegeInfoDao = new PrivilegeInfoDao();
        $privilegeInfos = $privilegeInfoDao->getListByGroupId($obj->getId());
        
        if($obj->getPrivilegeInfo()!=null){
            $privilegeInfoDao = new PrivilegeInfoDao();
            foreach($obj->getPrivilegeInfo() as $item){
                $Group = new UserGroup();
                $Group->setId($obj->getId());
                $Group->IsLoaded(true);
                $item->setGroup($Group);
                
                if(($item->getId() == null) || ($item->getId() == '')){
                    if($privilegeInfoDao->insertObject($item)==null) {
                        throw new Exception('Gagal menambahkan privilege info.');
                    }
                } else {
                    if($privilegeInfoDao->updateObject($item,$item->getId())==null) {
                        throw new Exception('Gagal mengupdate privilege info.');
                    }
                    if (($privilegeInfos != null) && (count($privilegeInfos) > 0)) {
                        $index_remove = -1;

                        foreach($privilegeInfos as $index=>$privilegeinfo) {
                            if ($privilegeinfo->getId() == $item->getId()) {
                                $index_remove = $index;
                                break;
                            }
                        }
                        if ($index_remove > -1) {
                            unset($privilegeInfos[$index_remove]);
                        }
                    }
                }
            }
           
            if (($privilegeInfos != null) && (count($privilegeInfos) > 0)) {
                foreach($privilegeInfos as $item) {
                    if (!$privilegeInfoDao->deleteObject($item->getId())) {
                        throw new Exception('Gagal menghapus unused privilege info.');
                    }
                }
            }
        }
        
        return $obj;
        
    }
    
    private function validateBase($obj) {
        if (is_null($obj)) { return false; }
        
        if (is_null($obj->getFunctionId()) || empty($obj->getFunctionId())) {
            $this->addError("Function Id is required!");
        }
        
        if (is_null($obj->getUrl()) || empty($obj->getUrl())) {
            $this->addError("URL is required!");
        }
        
        return $this->getServiceState();
    }
    
    private function validateObjectOnInsert($model) {
        if (is_null($model)) { return false; }
        $this->validateBase($model);
        return $this->getServiceState();
    }
    
    private function validateObjectOnUpdate($model) {
        if (is_null($model)) { return false; }
        $this->validateBase($model);
        return $this->getServiceState();
    }
    
}
?>